package com.sd.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sd.coupon.entity.Browse;
import com.sd.coupon.model.req.GoodsReq;
import com.sd.coupon.service.IBrowseService;
import com.sd.coupon.service.MBrowseService;
import com.sd.coupon.util.Contant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class MBrowseServiceImpl implements MBrowseService {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
    @Resource
    IBrowseService browseService;

    @Override
    public Set<Long> getNumIidListByUserId(Long userId){
        QueryWrapper<Browse> queryWapper = new QueryWrapper<>();
        queryWapper.eq(Contant.Browse.USER_ID,userId);
        queryWapper.orderByDesc(Contant.Browse.sort);
        List<Browse> browseItemIdList = browseService.list( queryWapper);

        if( browseItemIdList!=null && browseItemIdList.size()>0){

            Set<Long> numIidList = new HashSet<>();
            for (Browse c : browseItemIdList) {
                if(c!=null && c.getNumIid()!=null){
                    numIidList.add(c.getNumIid());
                }
            }
            return numIidList;
        }
        return null;
    }

    @Override
    public Boolean browseRecord(GoodsReq req){

        Browse collect = new Browse();
        collect.setNumIid(req.getNumIid());
        if (req.getUserId() == null) {
            collect.setUserId(1L);
        }else{
            collect.setUserId(req.getUserId());
        }

        boolean res = true;

        res = browseService.saveOrUpdate(collect);
        return res;
    }
}
